package com.linkhub.oms.admin.modules.order.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.thirdparty.publicsuffix.PublicSuffixType;
import com.linkhub.oms.admin.common.enums.OssObjectModuleEnum;
import com.linkhub.oms.admin.modules.order.domain.vo.orderpdf.OrderPdfVO;
import com.linkhub.oms.admin.modules.order.service.OrderFileService;
import com.linkhub.oms.admin.modules.order.service.OrderInfoAssembleService;
import com.linkhub.oms.admin.modules.oss.domain.vo.PutObjectResultVO;
import com.linkhub.oms.admin.modules.oss.service.AliyunOSSBizService;
import com.linkhub.oms.common.util.QtStreamUtil;
import com.linkhub.oms.component.freemarker.util.PdfUtil;
import com.linkhub.oms.dao.mpservice.IOrderService;
import com.linkhub.oms.entity.OrderEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @author Redball
 * @version 1.0.0
 * @Description OrderFileServiceImpl
 * @createTime 2024/7/11 上午2:36
 */
@Slf4j
@Service
public class OrderFileServiceImpl implements OrderFileService {

    @Resource
    private OrderInfoAssembleService orderInfoAssembleService;
    @Resource
    private FreeMarkerConfigurer freeMarkerConfiguer;
    @Resource
    private AliyunOSSBizService aliyunOSSBizService;
    @Resource
    private IOrderService orderService;

    private static final String ORDER_TEMPLATE = "order.ftl";
    private static final String ORDER_PDF_NAME_PRE = "Package_";

    @Override
    public Map<Boolean,List<OrderPdfVO>> createPackagePdfs(List<String> sysOrderIds) {
        List<OrderPdfVO> orderPdfVOs = orderInfoAssembleService.assembleOrderPdfInfos(sysOrderIds);
        if(CollUtil.isEmpty(orderPdfVOs)){
            return Maps.newHashMapWithExpectedSize(0);
        }
        return QtStreamUtil.partition(orderPdfVOs, this::createAndUploadPackagePdf);
    }

    @Override
    public boolean createAndUploadPackagePdf(OrderPdfVO orderPdfVO) {
        Map<String,Object> variables = Maps.newHashMap();
        variables.put("order",orderPdfVO);
        List<Map<String,Object>> listVars = Lists.newArrayList(variables);
        byte[] pdfBytes = PdfUtil.createPdfBytes(freeMarkerConfiguer, ORDER_TEMPLATE, listVars);
        if (ArrayUtil.isEmpty(pdfBytes)) {
            return false;
        }
        try(InputStream in = new ByteArrayInputStream(pdfBytes)) {
            PutObjectResultVO resultVO = aliyunOSSBizService.upload(OssObjectModuleEnum.ORDER_PACKAGE_FILE, in, getPackagePdfName(orderPdfVO));
            OrderEntity update = new OrderEntity();
            update.setPackagePdfUrl(resultVO.getUrl());
            orderService.updateOrderInfo(orderPdfVO.getSysOrderId(), update);
        } catch (Exception e){
            log.error(e.getMessage(),e);
            return false;
        }
        return true;
    }

    private String getPackagePdfName(OrderPdfVO orderPdfVO) {
        return ORDER_PDF_NAME_PRE+orderPdfVO.getSysOrderId()+".pdf";
    }
}
